package com.wandian.demo
{

	import com.wandian.demo.controller.GameBetController;
	import com.wandian.demo.controller.GameController;
	import com.wandian.demo.controller.LinesController;
	import com.wandian.demo.other.MyTweenEvent;
	import com.wandian.demo.other.test;
	import com.wandian.demo.ui.BetThumbnailsContainer;
	import com.wandian.demo.ui.SmallBetImage;
	import com.wandian.ui.controls.Button;
	import com.wandian.ui.controls.ClassStyleDeclaration;
	import com.wandian.ui.controls.Label;
	import com.wandian.ui.controls.LabelButton;
	import com.wandian.ui.controls.UIComponent;
	import com.wandian.ui.controls.UIManager;
	import com.wandian.ui.controls.WanButton;
	import com.wandian.ui.skins.ButtonSkin;
	import com.wandian.ui.skins.LabelSkin;
	
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.Strong;
	
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.Timer;
	import flash.utils.getQualifiedClassName;
	
	import flashx.textLayout.elements.BreakElement;
	
	import org.osmf.media.LoadableElementBase;
	
	import utils.AnimationButton;
	import utils.Debuger;
	import utils.Res;
	import utils.UIHelper;


	[SWF(backgroundColor="0x000000", width="800", height="600")]
	public class testslot extends Sprite
	{

		private var resDomain:ApplicationDomain=new ApplicationDomain(ApplicationDomain.currentDomain);
		public var m:Matrix=new Matrix();
		public var reelsArray1:Array=new Array();
		public var reelsArray2:Array=new Array();
		public var reelsArray3:Array=new Array();
		public var reelsArray4:Array=new Array();
		public var reelsArray5:Array=new Array();
		public var lastFrame:Date=null;
		public var stopFrame:Date=null;
		public var isInit:Boolean=false;
		//5个竖滚动条容器以及其遮罩层
		public var maskLayer:Sprite=new Sprite();
		public var selectorContainer:Sprite=new Sprite();
		public var btn_start:LabelButton;
		public var btn_stop:LabelButton;
		public var speed:Number;
		public var stopFlag:Number;
		public var stateID:Number;
		public var rollHeight:Number=310; //竖滚动条高度
		public var rollx:int=80;
		public var rolly:int=131;
		public var bottomBetIndex1:Number=0; //当前最底部押注的索引，从0开始
		public var bottomBetIndex2:Number=0; //当前最底部押注的索引，从0开始
		public var bottomBetIndex3:Number=0; //当前最底部押注的索引，从0开始
		public var bottomBetIndex4:Number=0; //当前最底部押注的索引，从0开始
		public var bottomBetIndex5:Number=0; //当前最底部押注的索引，从0开始
		public var betResult1:Number=0; //当前最底部押注的索引，从0开始
		public var betResult2:Number=0; //当前最底部押注的索引，从0开始
		public var betResult3:Number=0; //当前最底部押注的索引，从0开始
		public var betResult4:Number=0; //当前最底部押注的索引，从0开始
		public var betResult5:Number=0; //当前最底部押注的索引，从0开始
		private var maxSpeed:Number=100; //最大滚动速度
		private var currentSpeed1:Number=0; //第一注滚动速度
		private var currentSpeed2:Number=0; //第一注滚动速度
		private var currentSpeed3:Number=0; //第一注滚动速度
		private var currentSpeed4:Number=0; //第一注滚动速度
		private var currentSpeed5:Number=0; //第一注滚动速度
		private var scrollInterval:Number=0.4; //押注滚轮之间的时间间隔
		private var betThumbnails:Array=["skull", "pirate", "gun", "female", "bird", "butcher", "knife", "jewelry", "cannon", "table"];
		private var gameBetController:GameBetController=GameBetController.Instance;
		private var result15:Array=[];
		/**
		 *是否是定时器做动画
		 */
		private var boolTimer:Boolean=true;
		private var timer:Timer;
		private var startToStopTimer:Timer;
		private var addSpeed:Number=10;
		private var subSpeed:Number=5;
		private var stopPassImgCount:int=3; //停止命令接到后，滚轮需要pass掉的图片数
		private var tweenStopTime:int=2;
		private var doTweenState:Array=[0, 0, 0, 0, 0]; //0没有做，1，正在做，2，结束
		public var startX:int=75;
		private var startY:int=123; //屏幕起始y
		public var screenHeight:Number=320; //竖滚动条高度; //屏幕高度
		public var screenWidth:Number=646; //遮罩层宽度
		private var betThumbnailsContainers:Array=[]; //五个滚柱的父容器
		private var betPadding:Number=2;
		private var intervalTimer:Number=50;

		private var boolDoStartState:Array=[false, false, false, false, false];
		private var boolDoStopState:Array=[false, false, false, false, false];
		private var rollstartinterval:Number=300; //开始，滚柱间隔
		private var rollstopinterval:Number=1000; //开始，滚柱间隔
		private var startAndStopTime:Number=3000; //开始滚到5秒停止

		public function testslot()
		{
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onResLoadComplete);
			var context:LoaderContext=new LoaderContext(false, resDomain);
			loader.load(new URLRequest("res.swf"), context);
			//testScroll();
			Debuger.init(stage);
		}

		public function onResLoadComplete(e:Event):void
		{
			//初始化玩资源文件，就开始初始化定义样式
			Res.init(resDomain);
			UIManager.initialize(this.stage);
			UIHelper.initClassStyleDeclaration();
			UIHelper.addBitmapChild(this, "gamebg");
			//初始化滚动条start
			UIHelper.addSpriteChild(this, selectorContainer, null, this.startX, this.startY);
			UIHelper.addRectSpriteChild(this, maskLayer, "maskLayer", this.startX, this.startY, this.screenWidth, this.screenHeight, 0x2B2B2B, 1);

			//设置遮罩层，		
			selectorContainer.mask=maskLayer;
			for (var k:int=0; k < 15; k++)
			{
				var num:*=Math.round(Math.random() * (betThumbnails.length - 1));
				result15[k]=betThumbnails[num];
			}
			trace(result15);
			Debuger.print("一开始显示, " + result15);
			for (var m:int=1; m < 6; m++)
			{
				initBetScroll(m);
			}
			for (var i:int=1; i < 6; i++)
			{
				var betScrollArray:Array=getBetScrollByIndex(i);
				if (betScrollArray)
				{
					var bottomBetIndex:Number=getBottomBetIndex(i);
					SmallBetImage(betScrollArray[bottomBetIndex]).smallBetName=result15[(i - 1) * 3];
					SmallBetImage(betScrollArray[bottomBetIndex + 1 >= betScrollArray.length ? bottomBetIndex + 1 - betScrollArray.length : bottomBetIndex + 1]).smallBetName=result15[(i - 1) * 3 + 1];
					//防止索引超界
					SmallBetImage(betScrollArray[bottomBetIndex + 2 >= betScrollArray.length ? bottomBetIndex + 2 - betScrollArray.length : bottomBetIndex + 2]).smallBetName=result15[(i - 1) * 3 + 2];
				}
			}
			isInit=true;
			maxSpeed=110; //(reelsArray1[0].height + this.betPadding) / 2; //0.050秒移动的距离  1/2张
			addSpeed=12; //(reelsArray1[0].height + this.betPadding) / 16; //0.050秒移动的距离  加速 1/8 
			subSpeed=3; //(reelsArray1[0].height + this.betPadding) / 16; //0.050秒移动的距离  加速 1/8 
			trace("maxSpeed:" + maxSpeed + " addSpeed:" + addSpeed + " subSpeed:" + subSpeed);
			//初始化滚动条end
			//UIHelper.addSpriteChild(this, gameBetShowSprite, null, 0, 450);
			this.addChild(gameBetController);
			UIHelper.addBitmapChild(this, "choocecoin", 0, this.stage.stageHeight - 130);
			btn_selectline=UIHelper.addBAnimationButtonChild(this, "selectline", onselectlineBtnClikHandler, 275, this.stage.stageHeight - 105, -5, -5);
			btn_betperline=UIHelper.addBAnimationButtonChild(this, "betperline", onBetperlineBtnClikHandler, 165, this.stage.stageHeight - 105, -5, -5);
			btn_spin=UIHelper.addBAnimationButtonChild(this, "spin", onSpinBtnClikHandler, 370, this.stage.stageHeight - 105, -5, -5);
			btn_maxspin=UIHelper.addBAnimationButtonChild(this, "maxspin", onmaxspinBtnClikHandler, 522, this.stage.stageHeight - 105, -5, -5);
			btn_maxspin.setEnable(false);
			btn_spin.setEnable(false);
			//压线显示
			LinesController.Instance.showLinesContainer(this);
			setCallFun();
			this.stage.displayState=StageDisplayState.FULL_SCREEN;

		}

		/**
		 * 设置常用的回调函数
		 */
		private function setCallFun():void
		{
			gameBetController.spinEnableFun=this.setSpinEnable;

		}

		private var btn_maxspin:AnimationButton;
		private var btn_spin:AnimationButton;
		private var btn_betperline:AnimationButton;
		private var btn_selectline:AnimationButton;
		private var btn_choocecoin:AnimationButton;

		private function onmaxspinBtnClikHandler(event:MouseEvent):void
		{
			return;
			onStopClick(event);

		}

		private function setSpinEnable(btnName:String, boolEnable:Boolean):void
		{
			switch (btnName)
			{
				case "btn_spin":
					this.btn_spin.setEnable(boolEnable);
					break;
				case "btn_maxspin":
					this.btn_maxspin.setEnable(boolEnable);
					break;
				case "btn_selectline":
					this.btn_selectline.setEnable(boolEnable);
					break;
				case "btn_betperline":
					this.btn_betperline.setEnable(boolEnable);
					break;
				default:
					break;

			}
		}

		private function onBetperlineBtnClikHandler(event:MouseEvent):void
		{
			GameBetController.Instance.doBetPerLine();
		}

		private function onselectlineBtnClikHandler(event:MouseEvent):void
		{
			LinesController.Instance.doSelectLines();
		}

		//初始化5个滚动条
		public function initBetScroll(betRollIndex:Number):void
		{
			//初始化滚动条start		
			var betScrollArray:Array=getBetScrollByIndex(betRollIndex);
			if (betScrollArray == null || selectorContainer == null || maskLayer == null)
			{
				return;
			}
			var betx:Number=GameController.Instance.getRollXByIndex(betRollIndex);
			//将滚柱的5个容器加入被遮罩的容器里
			var smallimg:SmallBetImage=new SmallBetImage(betThumbnails[0]);
			var spriteContainer:BetThumbnailsContainer=new BetThumbnailsContainer(smallimg.width, (smallimg.height + this.betPadding) * this.betThumbnails.length);
			spriteContainer=UIHelper.addSpriteChild(selectorContainer, spriteContainer, "spriteContainer" + betRollIndex, betx, 0) as BetThumbnailsContainer;
			//方便按照索引取容器
			betThumbnailsContainers.push(spriteContainer);
			//将具体的10个图像全部加入各自的容器
			for (var j:int=0; j < betThumbnails.length; j++)
			{
				var small:SmallBetImage=new SmallBetImage(betThumbnails[j]);
				small.name=betThumbnails[j];
				betScrollArray.push(small);
				UIHelper.addSpriteChild(spriteContainer, small, betThumbnails[j] + j, 0, this.screenHeight - (j + 1) * (small.height + this.betPadding));
			}

		}
		private var whenstopTimerReapeat:Number=0;

		private var totalways:Array=[0, 0, 0, 0, 0];

		private function doTweenByTimer(rollIndex:Number, currentBottom:Number, betScrollArray:Array):void
		{
			var totalway:Number=this.startY + this.screenHeight - betScrollArray[currentBottom].y - betThumbnailsContainers[rollIndex - 1].y - this.selectorContainer.y + (stopPassImgCount) * (betScrollArray[currentBottom].height + this.betPadding); // - 2 * this.betPadding;
			var lastbottom:int=currentBottom + stopPassImgCount + 1;
			lastbottom=lastbottom > betScrollArray.length - 1 ? lastbottom - betScrollArray.length : lastbottom;
			//lastbottom=lastbottom >= reelsCount - 1 ? lastbottom - reelsCount + 1 : lastbottom;
			trace("列 " + rollIndex + " 收到停止,currentBottom " + currentBottom + " name:" + betScrollArray[currentBottom].name + " 将滑移距离:" + totalway + " 最终将停在:" + lastbottom + " name:" + betScrollArray[lastbottom].name + " 目标，当前坐标y:" + betScrollArray[lastbottom].y + " ,预计结束坐标：" + (betScrollArray[lastbottom].y + totalway));
			//将结果图修改		
			if (betScrollArray)
			{
				SmallBetImage(betScrollArray[lastbottom]).smallBetName=result15[(rollIndex - 1) * 3];
				SmallBetImage(betScrollArray[lastbottom + 1 >= betScrollArray.length ? lastbottom + 1 - betScrollArray.length : lastbottom + 1]).smallBetName=result15[(rollIndex - 1) * 3 + 1]; //防止索引超界
				SmallBetImage(betScrollArray[lastbottom + 2 >= betScrollArray.length ? lastbottom + 2 - betScrollArray.length : lastbottom + 2]).smallBetName=result15[(rollIndex - 1) * 3 + 2];
			}

			var n:Number=this.rollstopinterval / this.intervalTimer;
			var d:Number=(2 * totalway) / (n * (n - 1));
			var speedstop:Number=(2 * totalway) / n;
			var runway:Number=0;
			//trace("等差数列，求和：" + totalway + " a0 " + speedstop + " d:" + d);
			var timer:Timer=new Timer(this.intervalTimer, this.rollstopinterval / this.intervalTimer);
			timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerCompleteHandler);
			timer.start();
			doTweenState[rollIndex - 1]=2; //结束
			//trace("tween 状态更改："+rollIndex+" "+doTweenState[rollIndex - 1]);
			boolDoStartState[rollIndex - 1]=false;

			function onTimerHandler(event:TimerEvent):void
			{
				// TODO Auto-generated method stub
				var currentBottomt:Number=getBottomBetIndex(rollIndex);
				var betScrollArrayt:Array=getBetScrollByIndex(rollIndex);
				var currentSpeedt:Number=getCurrentSpeed(rollIndex);
				//trace(timer.currentCount + ":" + timer.repeatCount);
				runway+=speedstop;
				speedstop-=d;
				//trace("移动了：" + runway + " next speed:" + speedstop);
				for (var i:int=0; i < betScrollArrayt.length; i++)
				{
					betScrollArrayt[i].y+=speedstop + betPadding;
				}
				//trace("移完一次之后，betThumbnailsContainers[rollIndex - 1].y " + betThumbnailsContainers[rollIndex - 1].y + " 目标坐标," + betScrollArrayt[lastbottom].name + "y" + betScrollArrayt[lastbottom].y);
				changeBottomIndex(rollIndex, currentBottomt, betScrollArrayt);
			}
			function onTimerCompleteHandler(event:TimerEvent):void
			{
				// TODO Auto-generated method stub
				var currentBottomtt:Number=getBottomBetIndex(rollIndex);
				var betScrollArraytt:Array=getBetScrollByIndex(rollIndex);
				var currentSpeedtt:Number=getCurrentSpeed(rollIndex);
				var boolAllStop:Boolean=true;
				var dr:Number=screenHeight - betScrollArraytt[lastbottom].height - betPadding - betScrollArraytt[lastbottom].y
				for (var m:int=0; m < betScrollArraytt.length; m++)
				{
					betScrollArraytt[m].y+=dr;
				}
				//trace(rollIndex + "列 Tween 结束时候当前最低索引：" + currentBottomtt + " " + betScrollArraytt[currentBottomtt].name);
				for (var i:int=0; i < doTweenState.length; i++)
				{
					if (doTweenState[i] != 2)
					{
						boolAllStop=false;
						break;
					}
				}
				if (boolAllStop)
				{
					stopTimer();
//					btn_maxspin.removeBtnEventListener();
//					btn_spin.addBtnEventListener();
					//判断bet与credit
					if (gameBetController.checkMoney())
					{
						btn_spin.setEnable(true);

					}
					else
					{
						btn_spin.setEnable(false);
//						btn_selectline.setEnable(false);
//						btn_betperline.setEnable(false);
						trace("请压柱或投币");
					}
					btn_selectline.setEnable(true);
					btn_betperline.setEnable(true);
					btn_maxspin.setEnable(false);
				}
			}
		}

		/**
		 * 在滚柱收到停止消息时候，之前做tween特效，后来改成减速特效
		 * @param rollIndex
		 * @param currentBottom
		 * @param betScrollArray
		 */
		private function doTween(rollIndex:Number, currentBottom:Number, betScrollArray:Array):void
		{
			doTweenByTimer(rollIndex, currentBottom, betScrollArray);
			return;
			var totalway:Number=this.startY + this.screenHeight - betScrollArray[currentBottom].y - betThumbnailsContainers[rollIndex - 1].y - this.selectorContainer.y + (stopPassImgCount) * (betScrollArray[currentBottom].height + this.betPadding) - 2 * this.betPadding;
			var lastbottom:int=currentBottom + stopPassImgCount + 1;
			lastbottom=lastbottom > betScrollArray.length - 1 ? lastbottom - betScrollArray.length : lastbottom;
			//lastbottom=lastbottom >= reelsCount - 1 ? lastbottom - reelsCount + 1 : lastbottom;
			trace("列 " + rollIndex + " 收到停止,currentBottom " + currentBottom + " name:" + betScrollArray[currentBottom].name + " 将滑移距离:" + totalway + " 最终将停在:" + lastbottom + " name:" + betScrollArray[lastbottom].name);
			//将结果图修改		
			if (betScrollArray)
			{
				SmallBetImage(betScrollArray[lastbottom]).smallBetName=result15[(rollIndex - 1) * 3];
				SmallBetImage(betScrollArray[lastbottom + 1 >= betScrollArray.length ? lastbottom + 1 - betScrollArray.length : lastbottom + 1]).smallBetName=result15[(rollIndex - 1) * 3 + 1]; //防止索引超界
				SmallBetImage(betScrollArray[lastbottom + 2 >= betScrollArray.length ? lastbottom + 2 - betScrollArray.length : lastbottom + 2]).smallBetName=result15[(rollIndex - 1) * 3 + 2];
			}

			for (var i:int=0; i < betScrollArray.length; i++)
			{
				betScrollArray[i].y+=totalway;
			}
			totalways[rollIndex - 1]=totalway;
			this.betThumbnailsContainers[rollIndex - 1].rollIndex=rollIndex;
			this.betThumbnailsContainers[rollIndex - 1].currentBottom=currentBottom;
			this.betThumbnailsContainers[rollIndex - 1].betScrollArray=betScrollArray;
			doTweenState[rollIndex - 1]=2; //结束
			//trace("tween 状态更改："+rollIndex+" "+doTweenState[rollIndex - 1]);
			boolDoStartState[rollIndex - 1]=false;
			var boolAllStop:Boolean=false;
			for (var m:int=0; m < doTweenState.length; m++)
			{
				if (doTweenState[m] != 2)
				{
					boolAllStop=false;
					break;
				}
			}
			if (boolAllStop)
			{
				stopTimer();
				for (var k:int=0; k < doTweenState.length; i++)
				{
					doTweenState[k]=2;

				}
				this.btn_spin.addBtnEventListener();
			}
			//var myTween:Tween=new Tween(this.betThumbnailsContainers[rollIndex - 1], "y", Strong.easeIn, this.betThumbnailsContainers[rollIndex - 1].y - totalway, this.betThumbnailsContainers[rollIndex - 1].y,0.05);
			var myTween:Tween=new Tween(this.betThumbnailsContainers[rollIndex - 1], "y", Strong.easeOut, this.betThumbnailsContainers[rollIndex - 1].y - totalway, this.betThumbnailsContainers[rollIndex - 1].y, 1);
			myTween.addEventListener(TweenEvent.MOTION_FINISH, onTweenFinishHanlder);
			myTween.start();
			//changeBottomIndex(rollIndex, currentBottom, betScrollArray);
		}

		protected function onTweenFinishHanlder(event:TweenEvent):void
		{
			var myTween:Tween=event.target as Tween;
			var betThumbnailsContainer:BetThumbnailsContainer=myTween.obj as BetThumbnailsContainer;
			//trace(" Tween finish :" + betThumbnailsContainer.currentBottom);
			//trace(betThumbnailsContainer.rollIndex + "列 已经Tween结束");
			//trace(betThumbnailsContainer.rollIndex + "列 开始更换底部图");
			changeBottomIndex(betThumbnailsContainer.rollIndex, betThumbnailsContainer.currentBottom, betThumbnailsContainer.betScrollArray);
			trace(betThumbnailsContainer.rollIndex + "列 Tween 结束时候当前最低索引：" + betThumbnailsContainer.currentBottom + " " + betThumbnailsContainer.betScrollArray[betThumbnailsContainer.currentBottom].name);
			//trace(betThumbnailsContainer.rollIndex + "列 Tween 结束时候parent x,y,h,w " + betThumbnailsContainer.x + " " + betThumbnailsContainer.y + " " + betThumbnailsContainer.height + " " + betThumbnailsContainer.width);
			//trace(betThumbnailsContainer.rollIndex + "列 Tween 结束时，最底下 child x,y,h,w " + betThumbnailsContainer.betScrollArray[betThumbnailsContainer.currentBottom].x + " " + betThumbnailsContainer.betScrollArray[betThumbnailsContainer.currentBottom].y + " " + betThumbnailsContainer.betScrollArray[betThumbnailsContainer.currentBottom].height + " " + betThumbnailsContainer.betScrollArray[betThumbnailsContainer.currentBottom].width);


		}


		/**
		 * 摇滚指定的柱子
		 * @param rollIndex
		 */
		public function startRollBetScrolling(rollIndex:Number, currentBottom:Number, currentSpeed:Number, betScrollArray:Array):void
		{
			//start押注滚动柱滚动效果			
			var currentSpeed:Number=getCurrentSpeed(rollIndex); //(betScrollArray[currentBottom].height + this.betPadding) / 2; //		
			switch (doTweenState[rollIndex - 1])
			{
				case 0: //在加速阶段
					if (currentSpeed < this.maxSpeed)
					{
						currentSpeed+=this.addSpeed;
					}
					if (currentSpeed > this.maxSpeed)
					{
						currentSpeed=this.maxSpeed;
					}
					if (!this.btn_maxspin.getEnable())
					{
						if (boolMaxSpeed())
						{
							this.btn_maxspin.addBtnEventListener();
							this.btn_maxspin.setEnable(true);
						}
					}
					setCurrentSpeed(rollIndex, currentSpeed);
					break;
				case 1: //收到停止消息
					currentSpeed-=this.subSpeed;
					if (currentSpeed < 0)
					{
						currentSpeed=0;
						setCurrentSpeed(rollIndex, currentSpeed);
						return;
					}
					setCurrentSpeed(rollIndex, currentSpeed);
					break;

				default:
					return;
					break;

			}

			for (var m:int=0; m < betScrollArray.length; m++)
			{
				var bitmap:SmallBetImage=betScrollArray[m] as SmallBetImage;
				bitmap.y+=currentSpeed;
			}

			//trace("底部索引：" + currentBottom + " 名字:" + betScrollArray[currentBottom].name);
			//trace("底部图片信息，最底下 child x,y,h,w " + betScrollArray[currentBottom].x + " " + betScrollArray[currentBottom].y + " " + betScrollArray[currentBottom].height + " " + betScrollArray[currentBottom].width);

			changeBottomIndex(rollIndex, currentBottom, betScrollArray);

		}


		protected function onTimerHandler(event:TimerEvent):void
		{
			// TODO Auto-generated method stub		
			//trace("timer.currentCount " + timer.currentCount + " currentSpeed1:" + currentSpeed1 + " currentSpeed2:" + currentSpeed2 + " currentSpeed3:" + currentSpeed3 + " currentSpeed4:" + currentSpeed4 + " currentSpeed5:" + currentSpeed5);

			for (var i:int=1; i < 6; i++)
			{
				var currentBottom:Number=getBottomBetIndex(i);
				var betScrollArray:Array=getBetScrollByIndex(i);
				var currentSpeed:Number=getCurrentSpeed(i);
				if (doTweenState[i - 1] == 0 && !boolDoStartState[i - 1])
				{

					var intertimer:Number=(i - 1) * (200 / this.intervalTimer);
					if (this.timer.currentCount > intertimer) //	if (timer.currentCount > (i - 1) * 8) //8*0.05
					{
						boolDoStartState[i - 1]=true;
						trace("准备跑动列:" + i + " intertimer: " + intertimer);
					}
				}
				if (stopFlag == 1)
				{
					if (doTweenState[i - 1] == 0)
					{

						var intertime:Number=(i - 1) * (300 / this.intervalTimer);
						if (this.timer.currentCount - this.whenstopTimerReapeat > intertime) //	if (timer.currentCount > (i - 1) * 8) //8*0.05
						{
							//trace(i + "列准备停: intertime：" + intertime);
							doTweenState[i - 1]=1;

						}
					}
					if (doTweenState[i - 1] == 1 && currentSpeed < 37)
					{
						//trace(i + "列执行停: currentSpeed：" + currentSpeed);
						doTween(i, currentBottom, betScrollArray);
						continue; //break; //test 1
					}
					else if (doTweenState[i - 1] == 2)
					{
						continue; //break; //test 1
					}
						//stopTimer();

				}
				if (boolDoStartState[i - 1])
					startRollBetScrolling(i, currentBottom, currentSpeed, betScrollArray);
				continue; //	break; test 1
			}


		}

		private function changeBottomIndex(rollIndex:Number, currentBottom:Number, betScrollArray:Array):void
		{
			// TODO Auto Generated method stub
			for (var j:int=currentBottom; j < betThumbnails.length; j++)
			{
				if (betScrollArray[j].y + betThumbnailsContainers[rollIndex - 1].y + this.selectorContainer.y - this.startY - this.screenHeight >= 0)
				{
					var preIndex:int=j > 0 ? j - 1 : betScrollArray.length - 1;
					//trace(j + " " + betScrollArray[currentBottom].name + "上一个是 " + preIndex + " " + betScrollArray[preIndex].name);
					betScrollArray[j].y=betScrollArray[preIndex].y - betScrollArray[j].height - this.betPadding;
					currentBottom=j == betScrollArray.length - 1 ? 0 : j + 1;
						//trace("更换了底部currentBottom:" + currentBottom+" "+betScrollArray[currentBottom].name);
				}
				else
				{
					break;
				}

			}
			setBottomIndex(rollIndex, currentBottom);
		}

		private function setBottomIndex(rollIndex:int, currentBottom:int):void
		{
			// TODO Auto Generated method stub
			switch (rollIndex)
			{
				case 1:
					bottomBetIndex1=currentBottom;
					betThumbnailsContainers[rollIndex - 1].currentBottom=currentBottom;
					break;
				case 2:
					bottomBetIndex2=currentBottom;
					betThumbnailsContainers[rollIndex - 1].currentBottom=currentBottom;
					break;
				case 3:
					bottomBetIndex3=currentBottom;
					betThumbnailsContainers[rollIndex - 1].currentBottom=currentBottom;
					break;
				case 4:
					bottomBetIndex4=currentBottom;
					betThumbnailsContainers[rollIndex - 1].currentBottom=currentBottom;
					break;
				case 5:
					bottomBetIndex5=currentBottom;
					betThumbnailsContainers[rollIndex - 1].currentBottom=currentBottom;
					break;
				default:
					return;
			}
		}

		private function setCurrentSpeed(rollIndex:int, currentSpeed:Number):void
		{

			switch (rollIndex)
			{
				case 1:
					currentSpeed1=currentSpeed;
					break;
				case 2:
					currentSpeed2=currentSpeed;
					break;
				case 3:
					currentSpeed3=currentSpeed;
					break;
				case 4:
					currentSpeed4=currentSpeed;
					break;
				case 5:
					currentSpeed5=currentSpeed;
					break;
				default:
					break;
			}
		}

		private function getCurrentSpeed(rollIndex:int):Number
		{
			switch (rollIndex)
			{
				case 1:
					return currentSpeed1;

					break;
				case 2:
					return currentSpeed2;
					break;
				case 3:
					return currentSpeed3;
					break;
				case 4:
					return currentSpeed4;
					break;
				case 5:
					return currentSpeed5;
					break;
				default:
					break;
			}
			return 0;
		}

		public function getBetScrollByIndex(rollIndex:Number):Array
		{

			switch (rollIndex)
			{
				case 1:
					return reelsArray1; //第一卷滚动
					break;
				case 2:
					return reelsArray2; //第2卷滚动
					break;
				case 3:
					return reelsArray3; //第2卷滚动
					break;
				case 4:
					return reelsArray4; //第2卷滚动
					break;
				case 5:
					return reelsArray5; //第2卷滚动
					break;
				default:
					return null;
			}
		}

		public function onSpinBtnClikHandler(event:MouseEvent):void
		{
			LinesController.Instance.hiddenAllLines();
			onStartClick(event);

		}

		private function stopTimer():void
		{
			// TODO Auto Generated method stub
			if (timer != null)
			{
				if (timer.running)
				{
					timer.stop();
					timer.reset();
				}
				timer.removeEventListener(TimerEvent.TIMER, this.onTimerHandler);
			}
			for (var i:int=0; i < doTweenState.length; i++)
			{
				doTweenState[i]=0;
			}

		}


		private function startStartToStopTimer():void
		{
			// TODO Auto Generated method stub
			if (startToStopTimer == null)
			{
				startToStopTimer=new Timer(this.startAndStopTime,1);
				startToStopTimer.addEventListener(TimerEvent.TIMER, onstartToStopTimerHandler);
			}
			if (!startToStopTimer.running)			{
			
				startToStopTimer.start();
			}
		}
		
		protected function onstartToStopTimerHandler(event:TimerEvent):void
		{
			doStop();
			trace("倒计时到:次数"+startToStopTimer.currentCount);
			
		}
		private function stopStartToStopTime():void
		{
			// TODO Auto Generated method stub
			if (startToStopTimer != null)
			{
				if (startToStopTimer.running)
				{
					startToStopTimer.stop();
					startToStopTimer.reset();
				}
				startToStopTimer.removeEventListener(TimerEvent.TIMER, this.onstartToStopTimerHandler);
			}
			
		}
		
		private function startTimer():void
		{
			// TODO Auto Generated method stub
			if (timer == null)
			{
				timer=new Timer(intervalTimer);
			}
			if (!timer.running)
			{
				timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
				timer.start();
			}
		}


		/**
		 *
		 * @param event
		 */
		public function onStartClick(event:MouseEvent):void
		{
			if (!gameBetController.checkMoney())
			{
				btn_spin.setEnable(false);
				trace("请压柱或投币!");
				return;
			}
			trace("strat bet roll");
			for (var i:int=0; i < doTweenState.length; i++)
			{
				doTweenState[i]=0;
			}
			//event.currentTarget.removeEventListener(MouseEvent.CLICK, this.onSpinBtnClikHandler);
//			btn_spin.removeBtnEventListener();
			btn_spin.setEnable(false);
			btn_selectline.setEnable(false);
			btn_betperline.setEnable(false);
			gameBetController.subCreditAfterSpin(); //金额显示更新
			if (!boolTimer)
			{
				this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
			else
			{
				startTimer();
			}
			stopFlag=0;
			lastFrame=new Date();
			startStartToStopTimer();
		}


		public function onStopClick(event:MouseEvent):void
		{
			if (boolMaxSpeed())
			{
				doStop();
			}
			else
			{
				trace("滚轮还没有开始滚到最大速度，禁止停止！");
			}

		}

		private function doStop():void
		{
			trace("stop bet roll");
			//btn_maxspin.removeBtnEventListener();
			btn_maxspin.setEnable(false);
			this.stopFlag=1;
			if (this.timer)
				this.whenstopTimerReapeat=this.timer.currentCount;
			//随即五张图的结果位置
			for (var k:int=0; k < 15; k++)
			{
				result15[k]=betThumbnails[Math.round(Math.random() * (reelsArray1.length - 1))];
			}
			trace("结果将显示, " + result15 + " this.whenstopTimerReapeat:" + this.whenstopTimerReapeat);
			Debuger.print("结果将显示, " + result15);
		}

		private function boolMaxSpeed():Boolean
		{
			// TODO Auto Generated method stub
			if (this.currentSpeed1 < this.maxSpeed)
			{
				return false;
			}
//			else //test 1
//			{
//				return true;
//			}
			if (currentSpeed2 < this.maxSpeed)
			{
				return false;
			}
			if (currentSpeed3 < this.maxSpeed)
			{
				return false;
			}
			if (currentSpeed4 < this.maxSpeed)
			{
				return false;
			}
			if (currentSpeed5 < this.maxSpeed)
			{
				return false;
			}
			return true;
		}

		public function onEnterFrame(e:Event):void
		{
			if (isInit)
			{
				var thisFrame:Date=new Date();
				var seconds:Number=(thisFrame.getTime() - lastFrame.getTime()) / 1000.0;
				trace(seconds);
				trace("currentSpeed1:" + currentSpeed1 + " currentSpeed2:" + currentSpeed2 + " currentSpeed3:" + currentSpeed3 + " currentSpeed4:" + currentSpeed4 + " currentSpeed5:" + currentSpeed5);
				//如果停止状态且5个速度都停到0了，移除事件
				if (stopFlag == 1 && currentSpeed1 == 0 && currentSpeed2 == 0 && currentSpeed3 == 0 && currentSpeed4 == 0 && currentSpeed5 == 0)
				{
					this.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
				}

				//滚动5个押注
				for (var i:int=1; i < 6; i++)
				{
					//startRollBetScrolling(1);
					if (seconds > scrollInterval * (i - 1))
					{
						//startRollBetScrolling(i)
					}
				}


			}
		}

		public function getBottomBetIndex(rollIndex:Number):Number
		{
			switch (rollIndex)
			{
				case 1:
					return bottomBetIndex1;
				case 2:
					return bottomBetIndex2;
				case 3:
					return bottomBetIndex3;
				case 4:
					return bottomBetIndex4;
				case 5:
					return bottomBetIndex5;
				default:
					return 0;
			}
		}


	}
}
